package com.itheima03.day14;

/**
 * @author 刘全
 * @date 2021/8/6
 */
public class MyRunnable implements Runnable {
    private int tickets = 100;
    private Object obj = new Object();
    @Override
    public void run() {
        while (true) {
            synchronized (obj) {
                if (tickets <= 0) {
                    break;
                } else {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    tickets--;
                    System.out.println(Thread.currentThread().getName()+" 售票,还剩"+tickets+"张票");
                }
            }
        }
    }
}
